<template>
  <div class="admin-container">
    <div class="flexWithJusBetween mg-B20">
      <div class="title">企业资料</div>
      <div>
        <el-button type="primary" @click="addCompany">新增</el-button>
      </div>
    </div>
    <div v-show="queryType == false">
        <el-form ref="warehouseForm" label-width="80px" :inline="true">
          <el-form-item label="关键词">
            <el-input
              prefix-icon="el-icon-search"
              v-model="query.keyword"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="填报时间">
            <el-date-picker
              v-model="query.date"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-search"
            type="primary"
            @click="submitQuery"
            >查询
          </el-button>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-refresh-right"
            type="info"
            @click="resetQuery"
            >重置
          </el-button>
        </el-form>
      </div>
    <el-table border :data="tableData" style="width: 100%" align="center">
      <el-table-column prop="name" label="企业名称" align="center">
      </el-table-column>
      <el-table-column prop="linkman" label="负责人" align="center" >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="负责人联系方式"
        align="center"

      >
      </el-table-column>
      <el-table-column prop="registerAddress" label="注册地址" align="center">
      </el-table-column>
      <el-table-column prop="officeAddress" label="办公地址" align="center">
      </el-table-column>
      <el-table-column prop="createDate" label="填报时间" align="center">
        </el-table-column>
       <el-table-column label="企业状态" align="center" >
        <template slot-scope="scope">
          <div>
            {{ companyStatusFormate("company_status-" + scope.row.status) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="viewCompany(scope.row)"
            >查看</el-button
          >
          <el-button type="text" size="small" @click="auditCompany(scope.row)" v-if="scope.row.status==0||scope.row.status==3"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="applyUpdate(scope.row)" v-if="scope.row.status==2"
            >申请订正</el-button>
          
          <!-- <el-button type="text" size="small" @click="toProjectList(scope.row)"
              >项目资料填报</el-button
            > -->
        </template>
      </el-table-column>
    </el-table>
    <el-footer class="el-footerBox">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page.sync="pageNum"
        @current-change="getcompanyList"
      >
      </el-pagination>
    </el-footer>
  </div>
</template>

<script>
import permission from "@/directive/permission/index.js"; // 权限判断指令
import utils from "@/utils/utils.js";
import { list as companyList, update as companyUpdate, } from "@/api/company";
import { find as projectFind } from "@/api/project";
import { list as projectActivityList } from "@/api/projectActivity";

export default {
  directives: { permission },
  components: {},
  name: "companyList",
  data() {
    return {
      queryType: false,
      query: {
        keyword: "",
        date:"",
        startDate:"",
        endDate:""
      },
      total: 0,
      pageNum: 1,
      pageSize: 20,
      tableData: [],

      //字典信息
      companyStatusDict: [],
      companyStatusDictPromise: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {},
      immediate: true,
    },
  },

  beforeRouterLeave(to, from, next){
    console.log('企业列表to--', to);
    to.meta.keepAlive = false
    next()
  },

  created() {
    this.companyStatusDictPromise = utils.getDictList(
      "company_status",
      (res) => {
        this.companyStatusDict = res.data;
      }
    );
  },

  mounted() {
    let _this = this;

    Promise.all([this.companyStatusDictPromise]).then(
      (res) => {
        //console.log("字典已经全部加载完成--");
        _this.getcompanyList();
      }
    );
  },

  destroyed() {},

  methods: {
    //获取公司列表
    getcompanyList() {
      let _this = this;
      let params = new URLSearchParams();
      params.append("startDate", _this.query.startDate);
      params.append("endDate", _this.query.endDate);
      params.append("keyword", _this.query.keyword);
      params.append("pageNum", _this.pageNum);
      params.append("pageSize", _this.pageSize);
      companyList(params)
        .then((res) => {
          console.log("companyList成功获取数据--", res);
          if (res.type == "success") {
            this.tableData = res.data.data;
            this.total = res.data.total;
            if(this.tableData.length > 0){
              this.tableData.map(item => {
              item.createDate = utils.dateFormatter(item.createDate)
              })
              }
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },

    //条件查询
    submitQuery() {
      console.log('this.query.date--',this.query);
      if(!utils.isNull(this.query.date)){
        this.query.startDate = this.query.date[0]
        this.query.endDate = this.query.date[1]
      }
      this.getcompanyList();
    },

    //条件查询
    resetQuery() {
      this.query = {
        keyword: "",
        date:"",
        startDate:"",
        endDate:""
      },
        this.getcompanyList();
    },

    //查看公司
    viewCompany(data) {
      console.log("data--", data);
      this.$router.push({
        path: "/company/view",
        query: { companyId: data.id },
      });
    },

    //审核公司
    auditCompany(data) {
      console.log("data--", data);
      this.$store.dispatch("user/setCloseAliveName", 'companyEdit')
      this.$router.push({
        path: "/company/edit",
        query: { companyId: data.id },
      });
    },

    applyUpdate(data){
      console.log("data--", data);
      data.status = 4
      companyUpdate(data)
            .then((res) => {
              console.log("companySave成功获取数据--", res);
              if (res.type == "success") {
                this.getcompanyList();
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
    },

    //新增企业
    addCompany() {
      this.$store.dispatch("user/setCloseAliveName", 'companyAdd')
      this.$router.push({
        path: "/company/add",
      });
    },

    //项目资料填报
    toProjectList(data) {
      this.$router.push({
        path: "/project/list",
        query: { companyId: data.id },
      });
    },

    // //项目活动填报
    // toProjectActivityList(){
    //   this.$router.push({
    //     path: "/projectActivity/list",
    //     query: {companyId: data.id },
    //   });
    // },

    // //项目运营填报
    // toProjectExecuteList(){
    //   this.$router.push({
    //     path: "/projectExecute/list",
    //     query: {companyId: data.id },
    //   });
    // },

    companyStatusFormate(data) {
      return utils.selectDictLabel(this.companyStatusDict, data);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  display: block;
}
</style>
